/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
 */
package td.client.view;

import td.client.presenter.handlers.LoginJoinUiHandlers;
import td.client.ui.LabelFor;
import td.shared.Constants;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;

/**
 * @author Artem Shamsutdinov
 * 
 */
public class LoginJoinView extends
		AbstractPopupViewWithUiHandlers<LoginJoinUiHandlers> implements
		LoginJoinUiHandlers.MyView {

	@UiField
	TextBox thinkerIdText;
	@UiField
	PasswordTextBox passwordText;
	@UiField
	LabelFor emailLabel;
	@UiField
	TextBox emailText;
	@UiField
	CheckBox joinCheckbox;
	@UiField
	Button loginButton;
	@UiField
	Button joinButton;
	@UiField
	Label errorMessage;

	public interface Binder extends UiBinder<Widget, LoginJoinView> {
	}

	@Inject
	public LoginJoinView(EventBus eventBus, Binder binder) {
		super(eventBus);
		widget = binder.createAndBindUi(this);
	}

	@UiHandler("joinCheckbox")
	public void joinCheckboxClick(ClickEvent event) {
		if (joinCheckbox.getValue()) {
			emailText.setVisible(true);
			emailLabel.setVisible(true);
			joinButton.setVisible(true);
			loginButton.setVisible(false);
		} else {
			emailText.setVisible(false);
			emailLabel.setVisible(false);
			joinButton.setVisible(false);
			loginButton.setVisible(true);
		}
	}

	@UiHandler("joinButton")
	public void joinButtonClick(ClickEvent event) {
		getUiHandlers().submitJoinRequest(thinkerIdText.getText(),
				passwordText.getText(), emailText.getText());
	}

	@UiHandler("loginButton")
	public void loginButtonClick(ClickEvent event) {
		getUiHandlers().submitLoginRequest(thinkerIdText.getText(),
				passwordText.getText());
	}

	@Override
	public void onReveal() {
		errorMessage.setText(Constants.EMPTY);
		thinkerIdText.setValue(Constants.EMPTY);
		passwordText.setValue(Constants.EMPTY);
		emailText.setValue(Constants.EMPTY);
		joinCheckbox.setValue(false);
		emailText.setVisible(false);
		emailLabel.setVisible(false);
		joinButton.setVisible(false);
		loginButton.setVisible(true);
	}

	@Override
	public void onError(String errorMessageText) {
		errorMessage.setText(errorMessageText);
	}
}
